package com.xbxkj.api.app.mini.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xbxkj.api.app.mini.entity.MiniAppConst;
import com.xbxkj.commen.bean.layuiBean.ResultPage;
import com.xbxkj.commen.controller.entity.ApiCode;
import com.xbxkj.commen.controller.helper.$;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import com.xbxkj.commen.controller.model.ResultModel;
import com.xbxkj.commen.bean.UserAddress;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2020-01-02
 */
@RestController
@RequestMapping("/app/mini/{appid}/userAddress")
@Slf4j
@Api( tags="小程序用户收货地址相关")
public class UserAddressController extends MiniAppBaseController{

    /** 添加UserAddress
     * @param userAddress  前端传进的参数
     * @return 返回统一信息
     */
    @ApiOperation(value = "添加收货地址", notes = "添加收货地址")
    @PostMapping(value = "/saveUserAddress")
    @ApiImplicitParam(name = "token", value = "令牌" , paramType = "header", required = true)
    public ResultModel<?> saveUserAddress(HttpServletRequest request, @RequestBody  UserAddress userAddress  ){
        userAddress.setUserId(Integer.parseInt(request.getAttribute(MiniAppConst.userId).toString()));
        userAddress.setCreateTime(System.currentTimeMillis());
        return userAddressService.addUserAddress(userAddress);
    }

    /** 编辑UserAddress
     * @param userAddress  前端传进的参数
     * @return 返回统一信息
     */
    @ApiOperation(value = "修改收货地址", notes = "修改收货地址")
    @PostMapping(value = "/editUserAddress")
    @ResponseBody
    @ApiImplicitParam(name = "token", value = "令牌" , paramType = "header", required = true)
    public ResultModel<?> editUserAddress( @RequestBody UserAddress userAddress  ) throws Exception {
        if(userAddress.getId()==null){
            return  $.error(ApiCode.parameter_error,"参数错误，请传入收货地址的ID进行修改");
        }
             return userAddressService.editUserAddress(userAddress);
    }



    /** 删除一条UserAddress
     * @param id  前端传进的ID
     * @return 返回统一信息
     */
    @PostMapping(value = "/delUserAddress")
    @ApiOperation(value = "删除收货地址", notes = "删除收货地址")
    @ResponseBody
    @ApiImplicitParam(name = "token", value = "令牌" , paramType = "header", required = true)
    public ResultModel<?> delUserAddress( @RequestBody String id  ){
             return userAddressService.delUserAddress(id);
    }


    /** 查询UserAddress
     * @return 返回统一信息
     */
    @PostMapping(value = "/UserAddressList")
    @ApiOperation(value = "查询某用户收货地址", notes = "查询某用户收货地址")
    @ResponseBody
    @ApiImplicitParam(name = "token", value = "令牌" , paramType = "header", required = true)
    public ResultModel<?> UserAddressList(HttpServletRequest request ){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",request.getAttribute(MiniAppConst.userId).toString());
             return $.success(userAddressService.list(queryWrapper));
    }


}
